import etcd3
import json
import logging

logger = logging.getLogger(__name__)

class EtcdClient:
    def __init__(self, host='localhost', port=2379):
        self.client = etcd3.client(host=host, port=port)
    
    def put(self, key, value):
        if isinstance(value, dict):
            value = json.dumps(value)
        self.client.put(key, value)
    
    def get(self, key):
        value, metadata = self.client.get(key)
        if value is not None:
            return value.decode('utf-8')
        return None
    
    def delete(self, key):
        self.client.delete(key)
    
    def get_keys(self, prefix=''):
        return self.client.get_prefix(prefix)
    
    def get_prefix(self, prefix):
        keys = {}
        for value, metadata in self.client.get_prefix(prefix):
            key_str = metadata.key.decode('utf-8')
            value_str = value.decode('utf-8')
            keys[key_str] = value_str
        return keys